<?php
	//A TESTER
	require_once("Uploader.class.php");
	class ImageUploader extends Uploader{
		
		var $maxThumbWidth;
		var $maxThumbHeight;
		var $thumbDir;
		
		function ImageUploader($image,$dossier,$largeurVignette=0,$hauteurVignette=0,$dossierVignette =''){
			parent::__construct($image,$dossier);
			$this->maxThumbWidth = $largeurVignette;
			$this->maxThumbHeight = $hauteurVignette;
			$this->thumbDir = $dossierVignette;
		}
		
		function upload(){
			if($this->maxThumbWidth == 0) return parent::upload(); //Pas de vignette a faire
			if(!isset($this->uploadFile['tmp_name'])){
				foreach($this->uploadFile as $key => $value){
					$this->createThumb($this->uploadFile[$key]);
					if(!move_uploaded_file($this->uploadFile[$key]['tmp_name'], $this->dir)) return false;
				}
				return true;
			}
			else
			{
				$this->createThumb($this->uploadFile);
				return move_uploaded_file($this->uploadFile['tmp_name'], $this->dir);
			}
		}
		
		function createThumb($image){
			try{
				if(!list($imx, $imy, $type, $attr) = getimagesize($image['tmp_name'])) throw new Exception("ImageUploader : impossible d'analyser l'image ".$image['name']);
				switch($type){
					case 1:$src=imagecreatefromgif ($image['tmp_name']);break;
					case 2:$src=imagecreatefromjpeg ($image['tmp_name']);break;
					case 3:$src=imagecreatefrompng ($image['tmp_name']);break;
					default:throw new Exception("ImageUploader : Le type de l'image ".$image['name']." est inconnu ou non supporté.");
				}
				$rx=$imx/$this->maxThumbWidth;
				$ry=$imy/$this->maxThumbHeight;
				$thumb = ($rx>$ry)? array($imx/$rx,$imy/$rx):array($imx/$ry,$imy/$ry);
				$blank = imagecreatetruecolor (floor($thumb[0]),floor($thumb[1]));
				imagecopyresampled($blank,$src,0,0,0,0, floor($thumb[0]),floor($thumb[1]),$imx,$imy);
				switch($type){
					case 1:imagegif ($src,$this->thumbDir);break;
					case 2:imagejpeg ($src,$this->thumbDir);break;
					case 3:imagepng ($src,$this->thumbDir,100);break;
					default:throw new Exception("ImageUploader : Le type de l'image ".$image['name']." est inconnu ou non supporté.");
				}
			}
			catch (Exception $e)
			{
				echo "PHPFoxEngine Error : ".$e->getMessage();
			}
		}
	}
?>